package com.servlet;

import com.bean.BookBean;
import com.dao.BooksDao;
import com.dao.impl.BooksDaoImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/BookServlet2")

public class BookServlet2 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BooksDao booksDao=(BooksDao)ProxyUtil.getDaoProxy(new BooksDaoImpl());
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        JsonUtil jsonUtil =new JsonUtil();

        String currentPage = request.getParameter("currentPage");
        String bookname = request.getParameter("bookName");
        String booktype = request.getParameter("bookType");
        int i=  Integer.parseInt(currentPage);
        System.out.println(i);
        System.out.println(bookname);
        System.out.println(booktype);
        BookBean bookBean = new BookBean();
        bookBean.setBook_name(bookname);
        bookBean.setType_name(booktype);

        try {

            PageUtil<BookBean> bookBeanPageUtil = booksDao.queryDataPage(i, 15, bookBean);

            jsonUtil.setMsg("ok");
            jsonUtil.setData(bookBeanPageUtil);


        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String jsonstr = gson.toJson(jsonUtil);

        System.out.println(jsonstr);

        out.write(jsonstr);


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
 
 
 

